package com.dreamwhale.machinery.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.dao.PriceRangeMapper;
import com.dreamwhale.machinery.entity.PriceRange;
import com.dreamwhale.machinery.goods.entity.MachineShow;
import com.dreamwhale.machinery.goods.mapper.MachineShowMapper;
import com.dreamwhale.machinery.goods.service.MachineShowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 新机展 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-05-28
 */
@Service
public class MachineShowServiceImpl extends ServiceImpl<MachineShowMapper, MachineShow> implements MachineShowService {

    @Resource
    private MachineShowMapper machineShowMapper;

    @Resource
    private PriceRangeMapper priceRangeMapper;

    @Override
    public List<MachineShow> selectMachineShowsBYCondit(String showName, Integer brandId, Integer cateId, Integer priceId, Integer modelId) {

        QueryWrapper<MachineShow> queryWrapper = new QueryWrapper<>();
        if (showName != null) {
            queryWrapper.like("name", showName);
        }
        if (brandId != null) {
            queryWrapper.eq("brand_id", brandId);
        }

        if (modelId != null) {
            queryWrapper.eq("model_id", modelId);
        }
        if (cateId != null) {
            queryWrapper.eq("cate_id", cateId);
        }
        if (priceId != null) {
            QueryWrapper<PriceRange> price = new QueryWrapper<>();
            price.eq("price_range_id", priceId);
            List<PriceRange> priceRanges = priceRangeMapper.selectList(price);
            queryWrapper.between("price", priceRanges.get(0).getPriceRangeMin(), priceRanges.get(0).getPriceRangeMax());
        }
        queryWrapper.eq("del_flag", 0);

        return machineShowMapper.selectList(queryWrapper);
    }

    @Override
    public List<MachineShow> selectMacgineShowbyShowId(Integer showId) {
        return machineShowMapper.selectMacgineShowbyShowId(showId);
    }
}
